package com.geostar.constructionland.analyse.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.common.dao.LogAdjustDao;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.service.adjust.AdjustService;
import com.geostar.constructionland.common.entity.logs.LogAdjust;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author yelll
 * @since 2020-6-18 10:16
 **/
@Service
@Slf4j
public class BaseAdjustService {
    @Autowired
    LogAdjustDao logAdjustDao;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    AdjustService adjustService;

    public List<LandInfo> doAjust(AnalyseModel analyseModel, AdjustModel adjustModel) throws Exception {
        LogAdjust logAdjust = new LogAdjust();
        logAdjust.setId(UUID.randomUUID().toString().replace("-", ""));
        logAdjust.setFxbs(analyseModel.getAlyId());
        logAdjust.setFxlx(analyseModel.getFxlxMapped());
        try {
            logAdjust.setInput(objectMapper.writeValueAsString(adjustModel.getLandInfos()));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        Date startTime = new Date();
        logAdjust.setStartTime(startTime);
        List<LandInfo> adjustInfo = adjustService.adjust(adjustModel);
        logAdjust.setCostTime(System.currentTimeMillis() - startTime.getTime());
        logAdjustDao.saveLogAdjust(logAdjust);
        return adjustInfo;
    }
}
